// 判断数组是否包含相同的值
export const arrRepeat = (arr: any) => {
  let result = false;
  const str = ',' + arr.join(',') + ',';
  for (let i = 0; i < arr.length; i++) {
    if (str.replace(',' + arr[i] + ',', '').indexOf(',' + arr[i] + ',') > -1) {
      result = true;
      break;
    }
  }
  return result;
}

// 过滤html标签
export const removeHtmlTag = (str: string) => {
  if (!str && Number(str) != 0) return '';
  try {
    str = str
      .replace(/<\/?[^>]*>/g, '')
      .replace(/[ | ]*\n/g, '\n')
      .replace(/\n[\s| | ]*\r/g, '\n')
      .replace(/&nbsp;/gi, '')
      .replace(/\r\n/gi, '')
      .replace(/\n/gi, '');
    return str;
  } catch (ex) {
    return str;
  }
};

// 格式化value
export const valueFormat = (value: any): string[] => {
  if (value || value == 0) {
    // 数组
    if (Array.isArray(value)) {
      return value;
    }
    // 多个值
    if (String(value).indexOf(',') != -1) {
      return value.split(',');
    }
    // 单值
    return [value]
  }
  return [];
}

// 判断设置值是否在指定数组中
export const existIndataSource = (value: any, list: any[], selectValue: any, key: string) => {
  let _value = valueFormat(value);
  let newSelectValue = Array.isArray(selectValue) ? selectValue : [selectValue];
  _value = _value.filter((item: any) => !newSelectValue?.includes(item));
  let existArray = [];
  if (list?.length) {
    list.map((item: any) => {
      _value.map((item1: any) => {
        if (item?.[key] == item1) {
          existArray.push(item1);
        }
      });
    });
  }
  return _value.length == existArray.length;
};